package com.buiminhthang.food;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyBaseAdapter extends BaseAdapter {

	ArrayList<ListData> myList = new ArrayList<ListData>();
	LayoutInflater inflate;
	Context context;

	public MyBaseAdapter(Context context, ArrayList<ListData> myList) {
		this.myList = myList;
		this.context = context;
		inflate = LayoutInflater.from(this.context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return myList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return myList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MyViewHolder mViewHolder;
		if (convertView == null) {
			convertView = inflate.inflate(R.layout.activity_item_food, null);
			mViewHolder = new MyViewHolder();
			convertView.setTag(mViewHolder);
		} else {
			mViewHolder = (MyViewHolder) convertView.getTag();
		}

		mViewHolder.tvTittle = detail(convertView, R.id.tvTitle,
				myList.get(position).getTitle());
		mViewHolder.tvDesc = detail(convertView, R.id.tvDesc,
				myList.get(position).getDescription());
		mViewHolder.ivIcon = detail(convertView, R.id.ivIcon,
				myList.get(position).getImgResId());

		return convertView;
	}

	private class MyViewHolder {
		TextView tvTittle, tvPeople, tvTime;
		ImageView ivIcon;
	}

	private TextView detail(View v, int resId, String text) {
		TextView tv = (TextView) v.findViewById(resId);
		tv.setText(text);
		return tv;
	}

	private ImageView detail(View v, int resId, int icon) {
		ImageView iv = (ImageView) v.findViewById(resId);
		iv.setImageResource(icon); //

		return iv;
	}
}
